﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace sqxt
{
    public partial class beadroll : Form
    {
        public beadroll()
        {
            InitializeComponent();
        }

        bool size = false;
        SqlConnection conn = new SqlConnection(Dbconnect.s1);
        private void beadroll_Load(object sender, EventArgs e)
        {
            size = true;
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            #region 读取居委
            ArrayList arr = new ArrayList();
            SqlCommand com = new SqlCommand("select MedSite_name from medsite ", conn);
            conn.Open();
            SqlDataReader sdr = com.ExecuteReader();
            while (sdr.Read())
            {

                arr.Add(sdr.GetString(0));
            }
            foreach (string s in arr)
            {
                comboBox1.Items.Add(s);
            }
            sdr.Close();
            conn.Close();
            #endregion
            comboBox2.Items.Add("身份证");
            comboBox2.Items.Add("军官证");
            comboBox2.Items.Add("护照");
            comboBox2.Items.Add("回乡证");
            comboBox2.Items.Add("其他");
            toolStripLabel2.Text = fy.pageCurrent.ToString();
            toolStripLabel1.Text = "共0条记录  本页0条     共0页                            每页50行";
        }


     
        private void toolStripButton4_Click(object sender, EventArgs e)//登记体检
        {
            if (dataGridViewX1.Rows.Count>0)
            {

                try
                {
                    SqlCommand sc;
                    SqlDataReader sdr;
                    string healthno = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[4].Value.ToString();//获得档案编号
                    string n = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[0].Value.ToString();//姓名
                    #region  判断followup表是否有该居民数据
                    conn.Open();
                    sc = new SqlCommand("select * from followup where person_healthfile_no='" + healthno + "'", conn);
                    sdr = sc.ExecuteReader();
                    bool foll = sdr.HasRows;
                    conn.Close();
                    sdr.Close();
                    #endregion
                    conn.Open();
                    sc = new SqlCommand("select * from personfile1 where person_healthfile_no='" + healthno + "'", conn);
                    sdr = sc.ExecuteReader();
                    if (sdr.HasRows)
                    {
                        conn.Close();
                        sdr.Close();
                        if (!foll)//如果followup表中没记录，在表中插入一条记录
                        {
                            #region  在followup 表中存一条数据

                            SqlDataAdapter sda = new SqlDataAdapter("select * from followup", Dbconnect.s1);
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            dt.PrimaryKey = new DataColumn[] { dt.Columns["followup_id"] };
                            DataRow dr = dt.NewRow();
                            dr["person_healthfile_no"] = healthno;
                            dr["followup_id"] = AutoNumber.getAutoNumber("随访");//生成随访编号;
                            dr["followup_class"] = "健康体检";
                            dr["next_followup_date"] = DateTime.Now;
                            //dr["followup_doctor"] = comboBox5.Text;
                            dr["complete"] = "未完成";
                            dt.Rows.Add(dr);
                            SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                            sda.Update(dt);

                            #endregion
                        }
                        conn.Open();
                        sc = new SqlCommand("select followup_id from followup where person_healthfile_no='" + healthno + "' and  real_followup_date is null", conn);
                        sdr = sc.ExecuteReader();
                        sdr.Read();
                        string followupid = sdr[0].ToString();
                        conn.Close();
                        sdr.Close();
                        physical_check phy = new physical_check(healthno, n, followupid);
                        phy.Show();
                    }
                    else
                    {
                        MessageBox.Show("请先登记个人一般情况表");
                    }
                }
                catch (Exception e1)
                {
                    
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选中一条记录");
            }


        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ResidentAdd rda = new ResidentAdd();
            rda.Owner = this;
            rda.ShowDialog();
        }
       
        private void beadroll_Resize(object sender, EventArgs e) //大小改变事件
        {
            if (!size)
            {
                panel1.Height = 140;
                panel1.Width = listViewEx1.Width;
                groupBox1.Height = listViewEx1.Height - 155;
            }
            else
            {
                Mainform ma = new Mainform();
                panel1.Height = 140;
                groupBox1.Height = ma.Height -260;
                size = false;

            }
        }


        
        fenye fy = new fenye();
        private void buttonX1_Click(object sender, EventArgs e)
        {
            string medsite = "";
            if (comboBox1.Text.Trim() != "") { medsite = "  and Person_Medside='" + comboBox1.Text + "'"; }
            string sex = "";
            if (radioButton1.Checked) { sex = "  and person_sex='男'"; }
            if (radioButton2.Checked) { sex = "  and person_sex='女'"; }
            if (radioButton3.Checked) { sex = ""; }
            string create = "";
            if(radioButton7.Checked)  {create="  and person_iscreate='未建'";}
            if (radioButton8.Checked) { create = "  and  person_iscreate='已建'"; }
            if (radioButton9.Checked) { create = ""; }
            string time = "";
            if (checkBox2.Checked) { time = "  and  person_createdate  between  '" + DateTime.Parse(dateTimePicker1.Text) + "'  and  '" + DateTime.Parse(dateTimePicker2.Text)+"'"; }
            string birthdate = "";
            if (checkBox3.Checked) { birthdate = "  and  person_birthdate between  '" + DateTime.Now.AddYears(-Convert.ToInt32(textBox2.Text)).ToString() + "'  and  '" + DateTime.Now.AddYears(-Convert.ToInt32(textBox1.Text))+"'"; }
            string idtype = "";
            if (comboBox2.Text != "") { idtype = "  and  person_idtype='" + comboBox2.Text + "'"; }
            string id = "";
            if (textBoxX1.Text != "") { id = "  and  person_id_no='" + textBoxX1.Text + "'"; }
            string name = "";
            if (textBoxX2.Text != "") { name = "   and  person_name='" + textBoxX2.Text + "'"; }
           
            string sql;
            if (medsite == "" && sex == "" && create == "" && time == "" && birthdate == "" && id == "" && name == "")
            {
                sql = "select  person_name 姓名, person_sex 性别,person_idtype  证件类型,person_id_no 证件号码,person_healthfile_no  档案编号,person_birthdate 出生日期,person_family_tel  家庭电话,person_iscreate 建档状态,Person_addr 家庭地址,person_isend 档案状态 from  person ";
            }
            else
            {
                sql = "select  person_name 姓名, person_sex 性别,person_idtype  证件类型,person_id_no 证件号码,person_healthfile_no  档案编号,person_birthdate 出生日期,person_family_tel  家庭电话,person_iscreate 建档状态,Person_addr 家庭地址,person_isend 档案状态 from  person  where  person_healthfile_no <>''   " + medsite + sex + create + time + birthdate + idtype + id + name;
            }
            fy.feny(dataGridViewX1,bindingNavigator1,bindingSource1,sql);
            toolStripLabel2.Text = fy.pageCurrent.ToString();
            toolStripLabel1.Text = "共" + fy.nMax.ToString() + "条记录" + "  本页" + fy.nCurrent.ToString() + "条" + "     共" + fy.pageCount.ToString() + "页                           每页50行";
        }

        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            fy.fanye(toolStripButton10.Text);
            toolStripLabel2.Text = fy.pageCurrent.ToString();
            toolStripLabel1.Text = "共" + fy.nMax.ToString() + "条记录" + "  本页" + fy.nCurrent.ToString() + "条" + "     共" + fy.pageCount.ToString() + "页                           每页50行";
            
        }

        private void toolStripButton11_Click(object sender, EventArgs e)
        {
            fy.fanye(toolStripButton11.Text);
            toolStripLabel1.Text = "共" + fy.nMax.ToString() + "条记录" + "  本页" + fy.nCurrent.ToString() + "条" + "     共" + fy.pageCount.ToString() + "页                           每页50行";
        }

        private void toolStripButton8_Click(object sender, EventArgs e)//导出excel
        {
            string savePath = "";
            saveFileDialog1.Filter = "EXCEL(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                savePath = saveFileDialog1.FileName;
                expExcel.ExportData(dataGridViewX1, savePath);
            }
        }

        private void checkBox2_CheckedChanged_1(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;
            }
            else
            {
                dateTimePicker1.Enabled = false;
                dateTimePicker2.Enabled = false;

            }

        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                textBox1.Enabled = true;
                textBox2.Enabled = true;
            }
            else
            {
                textBox1.Enabled = false;
                textBox2.Enabled = false;
            }
        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        bool xiugai = false;
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (dataGridViewX1.Rows.Count > 0)
            {
                xiugai = true;
                string hno = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[4].Value.ToString();
                ResidentAdd rd = new ResidentAdd(xiugai, hno);
                rd.ShowDialog();
            }
            else
            {
                MessageBox.Show("请选择一条记录");
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {

            if (dataGridViewX1.Rows.Count > 0)
            {
                string hno = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[4].Value.ToString();
                person p = new person(hno);
                p.Show();
            }
            else
            {
                MessageBox.Show("请选择一条记录");
            }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)  //查看体检
        {
            if (dataGridViewX1.Rows.Count>0)
            {
                try
                {
                    
                    string healthno = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[4].Value.ToString();//获得档案编号
                    string n = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[0].Value.ToString();//姓名
                    conn.Open();
                    string sql = "select followup_id from followup where person_healthfile_no='" + healthno + "' and  real_followup_date =(select   max(real_followup_date)   from   followup where person_healthfile_no='" + healthno + "'  and real_followup_date<>'')";
                    SqlCommand sc = new SqlCommand(sql, conn);// and real_followup_date <> null
                    SqlDataReader sdr = sc.ExecuteReader();
                    if (sdr.HasRows)
                    {
                        sdr.Read();
                        string followupid = sdr[0].ToString();//最近一次健康体检的随访id
                        conn.Close();
                        sdr.Close();
                        bool ck = true;
                        physical_check phy = new physical_check(healthno, n, followupid, ck);
                        phy.Show();
                    }
                    else
                    {
                        MessageBox.Show("请先登记体检");
                    }
                }
                catch (Exception e1)
                {
                    
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选中一条记录");
            }

        }

        private void toolStripButton7_Click(object sender, EventArgs e)//建立个人一般情况表
        {
            try
            {
                if (dataGridViewX1.Rows.Count > 0)
                {
                    conn.Close();
                    string hno = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[4].Value.ToString();
                    conn.Open();
                    string sql = "select * from personfile1 where person_healthfile_no='" + hno + "'";
                    SqlCommand sc = new SqlCommand(sql, conn);// and real_followup_date <> null
                    SqlDataReader sdr = sc.ExecuteReader();
                    if (sdr.HasRows)
                    {
                        bool b = true;
                        personfile p = new personfile(b, hno);
                        p.Show();
                    }
                    else
                    {

                        personfile p = new personfile(hno);
                        p.Show();
                    }
                }
                else
                {
                    MessageBox.Show("请选择一条记录");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("出现未可预知问题");
            }
            

        }

        private void toolStripButton6_Click(object sender, EventArgs e)//登记随访
        {
            try
            {
                if (dataGridViewX1.Rows.Count > 0)
                {
                    string healthno = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[4].Value.ToString();
                    #region  判断followup表是否有该居民数据
                    conn.Close();
                    conn.Open();
                    SqlCommand sc = new SqlCommand("select * from followup where person_healthfile_no='" + healthno + "'", conn);
                    SqlDataReader sdr = sc.ExecuteReader();
                    bool foll = sdr.HasRows;
                    conn.Close();
                    sdr.Close();
                    #endregion
                    if (foll)
                    {

                        string hno = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[4].Value.ToString();
                        follow fo = new follow(hno);
                        fo.Show();
                    }
                    else
                    {
                        MessageBox.Show("该居民尚未随访信息或计划");
                    }
                }
                else
                {
                    MessageBox.Show("请选择一条记录");
                }
            }
            catch (Exception)
            {
            }
        }

       

       

     

 

    }
}
